import {batchDelete, deleteById1, findAll, findPage, insert, updateById, upload} from '@/api/goods/goods';
import {brandFindAll}from '@/api/goods/brand';
import {productfindPage} from '@/api/goods/product';
import {findlistColour}from '@/api/goods/colour'
import {findlistType,getTree}from '@/api/goods/type';
// import pickerOptions from '@/utils/date';
// import {base64} from "@/utils/base64Utils";
import showMessageBox from '@/utils/showMessageBox';

export default {
    data() {
        return {
            tableData:[],//商品表
            tableData1:[],//产品表
            id:0,
            total:0,//页面页数
            total1:0,//模态框里的页数
            batchIds:[],
            searchParams: {
                currentPage:1,
                pageSize:5,brandId:"",typeId:"",colourId:""},//商品查询条件
            searchParams1:{
                currentPage:1,
                pageSize:5,brandId:"",typeId:"",colourId:""},//产品查询条件
            dialogFormVisible:false,
            dialogFormVisible1:false,
            dialogFormVisible2:false,
            dialogFormVisible3:false,
            dialogFormVisible4:false,
            formData:{},//添加的集合
            formData3:{},//修改的集合
            imageUrl:'',//图片路径
            brandList:{},//品牌集合
            typeList:{},//类型集合
            colourList:{},//颜色集合
            type_options:[],
            goodstypename:[],//级联的值
        };
    },
    methods:{
        async getTerrs(){
            this.type_options = await getTree();

        }
        ,
        upload(formData){
            upload(formData).then(r => {
                console.log("r=============="+r);
                this.imageUrl = r;
                this.formData.goodsImg = r;
            });
        },
        handleChange(typeList){
            console.log(typeList.length);
            this.searchParams.typeId = typeList[typeList.length-1];
        },
        handleChange1(typeList){
            console.log(typeList.length);
            this.searchParams1.typeId = typeList[typeList.length-1];
        },

        //图片转base64码
        getImgStr(e){
            //上传文件到阿里云
            // const formData = new FormData();
            // formData.append("file",e.file);
            // this.upload(formData);
            // this.imageUrl = await base64.getBase64Str(e.file);

             //获取信息
             let fileReader = new FileReader();
             /*转换为base64*/
             fileReader.readAsDataURL(e.file)
             //发送
             fileReader.onload = e=> {
                 this.imageUrl=e.target.result;
                 this.formData.goodsImg = this.imageUrl;
                 this.formData3.goodsImg = this.imageUrl;
                 // this.formData.goodsImg.src=this.formData.goodsImg
             }

        },
        //判断修改还是添加
        async saveOrUpdate(){
            if (this.formData.id) {
                await this.updateByIdgoods(this.formData3)
            }else {
                await this.save(this.formData);
            }
            //  console.log("saveOrUpdate()");
            // await insert(this.formData);

        },
        //修改
        async updateByIdgoods(){
            let response = await updateById(this.formData3);
            if (response){
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
            }else {
                this.$message.error('修改失败');
            }
             await this.searchPage();
        },
        //添加
        async save(){
            let response = await insert(this.formData);
            if (response){
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
            }else {
                this.$message.error('添加失败');
            }
            this.searchPage();
            this.imageUrl = this.getImgStr(response.goodsImg);

        },
        //选择产品返回值
        async findById(row){
            console.log(row);
            this.formData = row;
            this.imageUrl = this.formData.goodsImg;
            this.formData.goodsName = row.modelId+row.colourName;
            this.formData.goodsId=row.goodsId;
            this.formData.brandId=row.brandId;
            this.formData.typeId=row.typeId;
            this.formData.colourId=row.colourId;


        },
        //  修改按钮获取值
        async goodsfindById(row){

            console.log(row);

            this.formData3 = row;
            this.imageUrl = this.formData3.goodsImg;
            this.formData3.id = row.id;
            this.formData3.goodsId=row.goodsId;
            this.formData3.brandId=row.brandId;
            this.formData3.typeId = row.typeId;
            this.formData.basicsPurchase = "集团采购";
            this.formData.basicsDistribution = "非铺货";
            this.formData.basicsStandbyMachine = "非备机";
            console.log( row.typeId);
            this.formData3.colourId=row.colourId;


        },
        selectChange(brandList1){
            if (brandList1.length == 1){
                this.formData.id = brandList1[0].id;
            }
            this.batchIds = brandList1.map(item=>item.id);
        },
        //删除
        showMsg(){
            showMessageBox.smb('此操作将永久删除该文件, 是否继续?', '删除成功!', '已取消删除').then(r  =>{
                r ? this.batchDeleteByIds():null
            })
        },
        //查所有
        selectAll(){
            findAll().then(response=>{
                this.tableData =response;
            })
        },



        //async 表示searchPage 是异步的，但是 await 表示 要等到 findPage 执行后，代码才会继续执行
        //分页查条件查询
        async searchPage(){
            // this.searchParams.typeId = this.goodstypename[this.goodstypename.length-1];
            let response = await findPage(this.searchParams);
            console.log(this.searchParams.typeId);
            // console.log(this.goodstypename);
            // console.log(this.goodstypename[this.goodstypename.length-1]);

                this.tableData = response.data;
                this.total=response.total;
        },
        //分页查条件查询
        async productPage(){
            // this.searchParams1.typeId = this.goodstypename[this.goodstypename.length-1];
            let response = await productfindPage(this.searchParams1);
                this.tableData1 = response.data;
                this.total1=response.total;
        },
        //分页默认值
        resetForm(){
            // this.options=typename.options;
            this.type_options=[];
            this.searchParams = {currentPage: 1,pageSize: 5};

            this.searchPage();

        },
        currentPageChange(currentIndex){
            this.searchParams.currentPage=currentIndex;
            this.searchPage();
        },
        //产品分页默认值
        productresetForm(){
            this.type_options=[]
            this.searchParams1 = {currentPage: 1,pageSize: 5}
            this.productPage();
        },
        productcurrentPageChange(currentIndex){
            this.searchParams1.currentPage=currentIndex;
            this.productPage();
        },
        async deleteById(row){
            // console.log(row.id);
            let response=await deleteById1(row.id);
            if (response){
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
            }else {
                this.$message.error('添加失败');
            }
            await this.searchPage();
        },
        async batchDeleteByIds() {
            await batchDelete(this.batchIds);
            await this.searchPage();
        },
        //查询品牌
        async findbrandList(){
            // console.log("aaa");
            let response = await brandFindAll();
                this.brandList = response;
        },
        //查询颜色
        async findcolourList(){
            let response = await findlistColour();
            // console.log(response);
            this.colourList = response;
        },
        //查询类型
        async findtypeList(){
            let response = await findlistType();
                this.typeList = response;
        }
    },
    created(){
        this.searchPage();
        this.findcolourList();
        this.findbrandList();
        this.findtypeList();
        this.getTerrs()
    }
};
